<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Adapter\Controller;
use Think\Controller;
use User\Api\UserApi;
use Think\Model;

class PurchaseController extends Controller {

    public function index(){
        if(session('userid')){
            $title="首页";
            $this->title=$title;
            $this->display();
        }else{
            $this->redirect('Purchase/login');
        }

    }
    public function login(){
        if($_SESSION['LOGIN']['name'] ){
            $this->assign('name',$_SESSION['LOGIN']['name']);
            $this->assign('password',$_SESSION['LOGIN']['password']);
        }
        $this->display();
    }
    public function gologin(){
        $username = $_POST['name'];
        $password = $_POST['password'];
        $map['nickname'] = $username ;
        $map['password'] = md5($password) ;
        $User = new UserApi;
        $uid = $User->login($username, $password);

        if(0 < $uid){ //UC登录成功
            /* 登录用户 */
            session('userid',$uid);
            session('username',$username);
            session('userid',$uid,'LOGIN');
            session('username',$username,'LOGIN');
            $this->success('登录成功！', U('Purchase/index'),0);

        } else { //登录失败
            switch($uid) {
                case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                case -2: $error = '密码错误！'; break;
                default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
            }
            $this->error($error);
        }
    }
    public function todaypurchase(){
        $map['status']=1;
        $goods=M('adapter_pre_purchase')->where($map)->select();
        $this->assign('_goods',$goods);
        $this->title="申请采购列表";
        $this->display();
    }
    public function addtopurchaselist(){
        $list = get_batch();
        if($list){
            $batch = $list['batch'];
            $map['id']=I('id');
            $addgoods = M('adapter_pre_purchase')->where($map)->find();
            $data['pid'] =$addgoods['pid'];
            $data['number'] =$addgoods['num'];
            $data['jin'] =$addgoods['jin'];
            $data['head'] =$addgoods['head'];
            $data['proName'] =$addgoods['proName'];
            $data['createdAt'] =time();
            $data['createdUser'] = session('userid');
            $data['updatedUser'] = session('userid');
            $data['updatedAt'] =time();
            $data['batch'] =$batch;
            $findpid = M('Purchase')->where(array("pid"=> $data['pid'],"batch"=>$batch))->find();
            if($findpid){
                $this->error('商品已存在预购中，请确认无误后删除');
            }else{
                M('Purchase')->add($data);
                M('adapter_pre_purchase')->where($map)->delete();
                $this->success('商品添加成功',U('Purchase/todaypurchase'));
            }
        }else{
            $this->error('暂无采购单，正在更新采购单',U('List/newlist'));
        }
    }
    public function deleteTodaypurchase(){
        $map['pid']=I('pid');
        $re=M('adapter_pre_purchase')->where($map)->delete();
        $proName=I('proName');
        $batch=1;
        if($re){
            add_log("D","删除商品".$proName,$batch);
            $this->success('删除成功',U('todaypurchase'));
        }else{
            $this->error('删除失败',U('todaypurchase'));
        }
    }
    public function havesee(){
        $map['id'] = I('id');
        $data['have_see'] = 1;
        if(M('Provider_report')->where($map)->save($data)){
            echo 1;
        }else{
            echo 0;
        }
    }

}
